/**
 * 日期工具函数
 * 为时间范围查询提供默认值，避免400错误
 */

/**
 * 获取当前年份的默认日期范围
 * @param year 年份，默认为当前年份
 * @returns 包含开始和结束日期的数组
 */
export const getDefaultYearRange = (year?: number): [string, string] => {
  const currentYear = year || new Date().getFullYear()
  return [`${currentYear}-01-01`, `${currentYear}-12-31`]
}

/**
 * 获取当前月份的默认日期范围
 * @param year 年份，默认为当前年份
 * @param month 月份，默认为当前月份
 * @returns 包含开始和结束日期的数组
 */
export const getDefaultMonthRange = (year?: number, month?: number): [string, string] => {
  const currentYear = year || new Date().getFullYear()
  const currentMonth = month || new Date().getMonth() + 1
  const monthStr = currentMonth.toString().padStart(2, '0')
  const lastDay = new Date(currentYear, currentMonth, 0).getDate()
  return [`${currentYear}-${monthStr}-01`, `${currentYear}-${monthStr}-${lastDay}`]
}

/**
 * 获取当前季度的默认日期范围
 * @param year 年份，默认为当前年份
 * @param quarter 季度，默认为当前季度
 * @returns 包含开始和结束日期的数组
 */
export const getDefaultQuarterRange = (year?: number, quarter?: number): [string, string] => {
  const currentYear = year || new Date().getFullYear()
  const currentQuarter = quarter || Math.ceil((new Date().getMonth() + 1) / 3)
  
  let startMonth: number
  let endMonth: number
  
  switch (currentQuarter) {
    case 1:
      startMonth = 1
      endMonth = 3
      break
    case 2:
      startMonth = 4
      endMonth = 6
      break
    case 3:
      startMonth = 7
      endMonth = 9
      break
    case 4:
      startMonth = 10
      endMonth = 12
      break
    default:
      startMonth = 1
      endMonth = 3
  }
  
  const startMonthStr = startMonth.toString().padStart(2, '0')
  const endMonthStr = endMonth.toString().padStart(2, '0')
  const endDay = new Date(currentYear, endMonth, 0).getDate()
  
  return [`${currentYear}-${startMonthStr}-01`, `${currentYear}-${endMonthStr}-${endDay}`]
}

/**
 * 获取最近N天的默认日期范围
 * @param days 天数，默认为30天
 * @returns 包含开始和结束日期的数组
 */
export const getDefaultRecentDaysRange = (days: number = 30): [string, string] => {
  const endDate = new Date()
  const startDate = new Date()
  startDate.setDate(endDate.getDate() - days)
  
  const formatDate = (date: Date): string => {
    return date.toISOString().split('T')[0]
  }
  
  return [formatDate(startDate), formatDate(endDate)]
}

/**
 * 格式化日期为 YYYY-MM-DD 格式
 * @param date 日期对象或日期字符串
 * @returns 格式化后的日期字符串
 */
export const formatDate = (date: Date | string): string => {
  if (typeof date === 'string') {
    return date
  }
  return date.toISOString().split('T')[0]
}

/**
 * 检查日期字符串是否有效
 * @param dateStr 日期字符串
 * @returns 是否有效
 */
export const isValidDate = (dateStr: string): boolean => {
  const date = new Date(dateStr)
  return !isNaN(date.getTime())
}

/**
 * 获取日期范围的天数
 * @param startDate 开始日期
 * @param endDate 结束日期
 * @returns 天数
 */
export const getDaysBetween = (startDate: string, endDate: string): number => {
  const start = new Date(startDate)
  const end = new Date(endDate)
  const diffTime = Math.abs(end.getTime() - start.getTime())
  return Math.ceil(diffTime / (1000 * 60 * 60 * 24))
} 